package com.example.administrator.cycleimage;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.example.administrator.cycleimage.wedgit.BannerImageViewUtils;
import com.example.administrator.cycleimage.wedgit.CycleViewPager;
import com.example.administrator.cycleimage.wedgit.ImageCycleViewListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Zyh on 2016/11/11.
 */
public class Main1Activity extends AppCompatActivity {
    private CycleViewPager mCycleViewPager;
    private Context mContext;
    private List<String> mImgs=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext=Main1Activity.this;
        mCycleViewPager = (CycleViewPager) getSupportFragmentManager().findFragmentById(R.id.life_fragment_banner);
        updateData();
        initBanner();
    }
    public void updateData(){
        OkHttpClient mOkhttpClient=new OkHttpClient();
        Request mRequest=new Request.Builder().url("http://192.168.8.4/tp3/api.php/Home/Goods/getBanner").build();
        mOkhttpClient.newCall(mRequest).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonStr=response.body().string();
                Log.d("---","获取的数据是："+jsonStr);
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        String imagUrl=  jsonArray.getString(i);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    private ImageView bannerImageView;
    private List<ImageView> mImageViews=new ArrayList<>();
    private void initBanner() {

        bannerImageView= BannerImageViewUtils.getImageView(mContext);

        bannerImageView.setImageResource(R.drawable.life_banner);
        mImageViews.add(bannerImageView);
        //设置循环，在调用的setData方法之前调用
        mCycleViewPager.setCycle(false);
        /**
         * 加载轮播的数据的setData方法是核心方法
         * 三个参数：
         * 1.List<ImageView> view 轮播图片imageView集合
         * 2.List<T> list 轮播imageview对应的数据（图片服务器地址，广告的内容链接，链接）
         * 3.ImageCycleViewListener lister  每个轮播imageView对应的点击事件
         * 如果轮播是静态的，imageView的集合和对应的T数据是相等
         * 如果轮播是循环播放的，那么imageView的集合比对应的T数据多两个
         * 多出的两个跟循环的原理相关，在list<imageView>头部增加一个imageView和尾部增加一个imageView
         */
        List<String> mDatas=new ArrayList<>();
        mDatas.add("大家叫我华哥");
        mCycleViewPager.setData(mImageViews, mDatas, new ImageCycleViewListener<String>() {
            @Override
            public void onImageClick(String info, int position, View imageView) {
                Log.d("---",info+"-----"+position);
            }
        });
        mCycleViewPager.setWheel(true);
        mCycleViewPager.setIndicatorCenter();
    }
}
